#!/usr/bin/env python
import appuifw
import e32
if e32.in_emulator():
    import gtk


class uitest(object):
    def __init__(self):
        appuifw.app.title = u"Test"
        appuifw.app.screen = "normal"
        appuifw.app.exit_key_handler = None

        #self._list = appuifw.Listbox()
        #self._canvas = appuifw.Canvas()
        self._makeMainMenu()
        self._makeText()
        self._makeCanvas()
        self._setTabs()
        self._tcb(0)

    def _mm_screen_cb(self, x):
        appuifw.app.screen = x
	
    def _mm_orientation_cb(self, x):
        appuifw.app.orientation = x
	
    def _mm_query_cb(self, qtype, defval = None):
        res = appuifw.query(u"Query test label:", qtype, defval)
        if res == None:
            appuifw.note(u"Query returned None")
        else:
            appuifw.note(u"Query ret: "+str(res))

    def _mm_multiquery_cb(self):
        res = appuifw.multi_query(u"User Name:", u"Password:")	
        if res != None:
	        appuifw.note(res[0]+", "+res[1])
	    
    def _mm_popup_cb(self, x):
        if x == 0:
            lst = [u"i1",u"i2",u"i3",u"i4",u"i5",u"i6",u"i7",u"i8",u"i9"]
            res = appuifw.popup_menu(lst, u"Single menu")
        else:
            lst = [(u"i11",u"i12"),(u"i21",u"i22"),(u"i31",u"i32"),(u"i41",u"i42")]
            res = appuifw.popup_menu(lst, u"Double menu")
	    
        appuifw.note("popup res: "+str(res))
	
    def _mm_sellist_cb(self):
        lst = [u"i1",u"i2",u"i3",u"i4",u"i5",u"i6",u"i7",u"i8",u"i9"]
        res = appuifw.selection_list(lst, 1)
        if res != None:
	        appuifw.note(u"Selected elt: "+str(res))
	
    def _mm_multisel_cb(self, x):
        lst = [u"i1",u"i2",u"i3",u"i4",u"i5",u"i6",u"i7",u"i8",u"i9"]
        res = appuifw.multi_selection_list(lst, x, 1)
        if res != None:
	        appuifw.note(u"Selected elt: "+str(res))

    def _makeMainMenu(self):
        self._main_menu = [
#	        (u"query", self._mm_query_cb),
	        (u"query",  ((u"text", lambda :self._mm_query_cb("text", "default text")),
                         (u"code", lambda :self._mm_query_cb("code", "default code")),
                         (u"number", lambda :self._mm_query_cb("number", 12345)),
			             (u"date", lambda :self._mm_query_cb("date", 939070800.0)),
			             (u"time", lambda :self._mm_query_cb("time", 45000.0)),
			             (u"query", lambda :self._mm_query_cb("query")),
			             (u"float", lambda :self._mm_query_cb("float", 12.56)),
                         )),
	        (u"multi_query", self._mm_multiquery_cb),
            (u"note", lambda : appuifw.note(u"note test text", "error")),
	        (u"popup_menu", ((u"single n/l", lambda :self._mm_popup_cb(0)),
                             (u"double w/l", lambda :self._mm_popup_cb(1)),
                             )),
	        (u"selection_list", self._mm_sellist_cb),
	        (u"multi selection", ((u"checkmarks", lambda :self._mm_multisel_cb("checkmark")),
                                  (u"checkboxes", lambda :self._mm_multisel_cb("checkbox")),
                                  )),
            (u"screen", ((u"normal", lambda :self._mm_screen_cb("normal")),
                         (u"large", lambda :self._mm_screen_cb("large")),
                         (u"full", lambda :self._mm_screen_cb("full")),
                         )),
	        (u"orientation", ((u"automatic", lambda : self._mm_screen_cb("automatic")),
                             (u"portrait", lambda : self._mm_screen_cb("portrait")),
                             (u"landscape", lambda : self._mm_screen_cb("landscape")),
                             ))
            ]
		    
    #--------------------- tabs part ------------------------------------
    def _tcb(self, x):
        if x == 0:
            appuifw.app.body = None
            appuifw.app.menu = self._main_menu
        elif x == 1:
            appuifw.app.body = self._text
            appuifw.app.menu = self._text_menu
        elif x == 3:
            appuifw.app.body = self._canvas
            appuifw.app.menu = self._canvas_menu
             
        appuifw.note(u"tab item")

    def _setTabs(self):
        appuifw.app.set_tabs([u"Main", u"Text",u"List",u"Canvas"], self._tcb)
        appuifw.app.activate_tab(0)

    #------------------------------- text part -------------------------
    def _text_add_cb(self):
        appuifw.app.body.add(u" 1234567890")

    def _text_clearset_cb(self):
        appuifw.app.body.clear()
        appuifw.app.body.set(u" v lesu radilas' jelochka, la-la, la-la, la-la!!!")
        appuifw.app.body.set_pos(2)

    def _text_style(self, style):
        if style == 1:
            print "style1"
            self._text.color = (100,0,0)
            self._text.highlight_color = (255,255,100)
            self._text.style = appuifw.STYLE_BOLD | appuifw.STYLE_ITALIC | appuifw.STYLE_UNDERLINE | appuifw.HIGHLIGHT_STANDARD
                           
        elif style == 2:
            print "style2"
            self._text.color = (80,0,80)
            self._text.highlight_color = (100,100,100)
            self._text.style = appuifw.STYLE_STRIKETHROUGH | appuifw.HIGHLIGHT_STANDARD
            self._text.font = u"dense"
                             

    def _makeText(self):
        self._text = appuifw.Text()
        self._text.set(u'This is a simple text')
        self._text.color = (100,0,100)
        self._text.highlight_color = (200,200,200)
        self._text.style = appuifw.STYLE_BOLD | appuifw.STYLE_ITALIC | appuifw.STYLE_UNDERLINE
        self._text.add(u' 1234567890')

        self._text_menu = [
            (u"Add", self._text_add_cb),
            (u"Clear/set", self._text_clearset_cb),
            (u"Style", ((u"Style 1", lambda : self._text_style(1)),
                        (u"Style 2", lambda : self._text_style(2))
                        ))
            ]
        

    #-------------------- listbox part -----------------------------------------
    def _makeList(self):
        #listItems = ((u"litem1", u"111"),
        #             (u"litem2", u"222"),
        #             (u"litem3", u"333"),
        #             (u"litem4", u"444"),
        #             (u"litem5", u"555"))
        #appuifw.app.body = appuifw.Listbox(listItems,None)
        pass

    def _canvas_redraw_cb(self, rect):
        self._canvas.pieslice(
	    [(30,30),(70,70)],
	    1, 3.14,
	    outline=(255,0,0),
	    fill=(0,255,0))

    def _canvas_draw_cb(self, x):
        if x == 0:
            self._canvas.ellipse(
                [(30,30),(70,70),(50,120),(80,200)],
		        outline=(40,255,150),
                fill=(0,255,0))
        elif x == 1:
            self._canvas.rectangle(
                [(10,40),(30,150),(200,120),(250,140),(150,150),(160,160)],
		        outline=(40,255,150),
                fill=(0,0,255))
        elif x == 2:
            self._canvas.polygon(
                [(30,30),(70,70),(50,120),(80,200)],
                outline=(200,255,0))
        elif x == 3:
            self._canvas.arc(
                [(90,130),(120,170)],
                -3, -0.2,
                outline=(40,255,150),
		        fill=(128,128,128))
        elif x == 4:
            self._canvas.text(
                [(90,130),(120,170)],
                u"Test text",
                (40,255,150))
            
    def _makeCanvas(self):
        self._canvas = appuifw.Canvas(self._canvas_redraw_cb)

        self._canvas_menu = [
            (u"Clear", lambda : self._canvas.clear((0,0,0))),
            (u"2 Ellipses", lambda : self._canvas_draw_cb(0)),
            (u"3 Rect", lambda : self._canvas_draw_cb(1)),
            (u"Poly", lambda : self._canvas_draw_cb(2)),
            (u"Arc", lambda : self._canvas_draw_cb(3)),
            (u"Text", lambda : self._canvas_draw_cb(4))
            ]
         
                                                                                                          
    def run(self):
        if e32.in_emulator():
            gtk.main()
        else:
            lock = e32.Ao_lock()
            lock.wait()



test = uitest()
test.run()
